<%--
  Created by IntelliJ IDEA.
  User: 86183
  Date: 2022/5/3
  Time: 19:52
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <link rel="shortcut icon" href="#"/>
    <title>登录</title>
    <style>
        body{
            color: white;
            background-color: black;
        }
        a{
            color: white;
            text-decoration: none;
            font-size: 15px;
        }
    </style>
</head>
<body>
<div class="center">
    <form  method="post">
        昵称<input type="text" name="userName" id="userName"/>
        <br>
        密码<input type="password" name="password" id="password"/>
        <br>
        <button type="button"  id="enroll" onclick="enrolls()">登录</button>
        <br>
        <a href="Register.jsp">没有账号? 点击此处注册</a>
    </form>
</div>
</body>
<script>
    function setCookie(name,value,exdays) {
        let date = new Date();
        date.setTime(date.getTime() + (exdays*24*60*60*1000));
        let expires = "expires=" + date.toUTCString();
        document.cookie = name + "=" + value + "; " + expires;
    }
    function getCookie(name) {
        if (document.cookie.length > 0) {
            let start = document.cookie.indexOf(name + "=");
            if (start != -1) {
                start = start + name.length + 1;
                let end = document.cookie.indexOf(";", start);
                if (end == -1) end = document.cookie.length;
                return decodeURI(document.cookie.substring(start, end));
            }
        }
        return null;
    }
    function enrolls(){
        let name=document.getElementById("userName").value;
        let password=document.getElementById("password").value;
        console.log(name)
        console.log(password)
        if(name==''){
            alert("信息不完整!");
            return;
        }
        if(password==''){
            alert("信息不完整!");
            return;
        }
        let User={
            "userName" :name,
            "password" :password,
            "method":"enroll"
        }
        let xhr = new XMLHttpRequest();
        xhr.open("post",
            "http://localhost:80/enroll",
            true);
        xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded')
        xhr.send("json="+JSON.stringify(User));
        xhr.onreadystatechange=function (){
            if(xhr.status==200 && xhr.readyState==4){
                let data = xhr.responseText;
                console.log("后端传过来:"+data);
                let json=JSON.parse(data)
                console.log(json)
                if(json.code==404){
                    alert(json.state)
                    window.location="index.jsp";
                }else if(json.code==200 && json.state==='SUCCESS'){
                    let role=JSON.parse(json.data);
                    console.log(role)
                    console.log(role["role"])
                    alert(json.state)
                    setCookie("role",role["role"],7)
                    setCookie("id",role["id"],7)
                    setCookie("name",name,7)
                    if(role["role"]=='商家'){
                        window.location="Seller.jsp";
                    }else{
                        window.history.back();
                    }
                }
            }
        }
    }
</script>
</html>
